<?php
require_once Yii::app()->basePath . "/extensions/phpquery/phpQuery/phpQuery.php";

/**
 * 爬取
 * @author xiaojy xiaojy@wanthings.com
 */
class ImgCommand extends CConsoleCommand
{
    public function actionBegin()
    {
        $num = 1;
        while ($num < 2) {
            $this->downImg($num);
            sleep(0.01);
            echo "end " . $num . " time\n";
            $num++;
        }
    }


    //下载图片
    public function downImg($num)
    {


        header('Content-type:image/jpeg');

        $url = "http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1509687528466_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E5%A3%81%E7%BA%B8";
        $cookie = "BDIMGISLOGIN=0; winWH=%5E6_1350x661; BDqhfp=%E5%A3%81%E7%BA%B8%26%260-10-1undefined%26%260%26%261; BAIDUID=E57DD0078E3C8DB7E8B6A3E00C18B5D4:FG=1; BIDUPSID=E57DD0078E3C8DB7E8B6A3E00C18B5D4; PSTM=1507515692; BDUSS=FiMkpmUXN-Mm5pLVhtbnRMaWRMTGJpLTdlaGdpaHp0dzk1azNLSDJ0a2l-d1JhSVFBQUFBJCQAAAAAAAAAAAEAAADqeM4dtKyzpLXEtbm5s7zFxK8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACJy3Vkict1ZWn; __cfduid=d8e76706aeaaca2de1b01ec87e18603b31508742978; indexPageSugList=%5B%22%E5%A3%81%E7%BA%B8%22%5D; cleanHistoryStatus=0; BDSFRCVID=_1LsJeC62iKQqknZpKZWTxPf2mXXACRTH6aoLXyeLp7vJss1bsU0EG0PDf8g0Ku-Nb29ogKKXgOTHw3P; H_BDCLCKID_SF=tRAOoC_2tCv0jb7kbP5HhICV-frb-C62aKDs34-E-hcqEIL4jM5cLn8BWl3iXjJnMNRhotovWJ--jxbSj4Qoetn3D4TxQhRGKHvZhqbP-l5nhMJlXj7JDMP0qJ-HWMry523ihn3vQpnbhUtuDj0hjTbXeHts-bbfHDrKXJnaK4ThKROvhjRrDfAyyxomtjj0Wa6DBbru-pvASUb90-DKhqkRLtbNLUkqKCOrhPTHBPtbflcRKTQDKpQQQttjQU3hfIkja-KEBJ_KbJ7TyURibU47yhteQTT2-DA__CtbJK5P; BDRCVFR[itSY6gOFmMf]=_S4e-Z-wQHsnjDvnWTzQhPEUf; PSINO=3; H_PS_PSSID=";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_REFERER, 'http://www.baidu.com');
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        $data = curl_exec($ch);
        if ($data == false) {
            echo 2;
            sleep(10);
        }
        curl_close($ch);
        phpQuery::newDocumentHTML($data);//加载页面

        $tp = fopen("K:/png/bizhi/" . "3232" . "html", "a");
        fwrite($tp, $data);
        fclose($tp);
        exit;

        foreach (pq(" li ") as $key => $value) {
            //获取href。

            echo 'list';
            $temp = pq("img", $value)->attr("data-imgurl"); //图片链接

            echo $temp;
            $ext = ".png";
            $status = strripos($temp, ".png");
            if ($status != false) {
                $ext = ".png";
            }
            $status = strripos($temp, ".jpg");
            if ($status != false) {
                $ext = ".jpg";
            }
            $status = strripos($temp, ".gif");
            if ($status != false) {
                $ext = ".gif";
            }

            //下载图片
            $url = $temp;
            echo $url;
            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

            $imageData = curl_exec($curl);
            $hash = md5($url);
            curl_close($curl);

            $tp = fopen("K:/png/bizhi/" . $hash . $ext, "a");
            fwrite($tp, $imageData);
            fclose($tp);
        }
    }

}